package main

import (
	"flag"
	"gostore/config"
	"gostore/controller"
	"gostore/dao"
	"gostore/router"
	"gostore/util/log"
	"net/http"
	"os"
	"time"
)

var global *params

type params struct {
	*flag.FlagSet `json:"-"`

	configPath string

	debug bool
}

func main() {
	l := log.Factory(main)
	l.Info("gostore running")

	global = &params{}
	global.FlagSet = flag.NewFlagSet("Gostore Global Params", flag.ContinueOnError)

	global.StringVar(&global.configPath, "configPath", "app.yml", "config file path")

	global.BoolVar(&global.debug, "debug", false, "open debug log")

	var err error

	err = global.Parse(os.Args[1:])
	if err != nil {
		os.Exit(0)
	}

	// configPath := global.configPath
	cfg, err := config.ReadYaml()
	if err != nil {
		log.Fatal(err.Error())
	}

	config.Init(cfg)

	dao.Open(cfg.Database)

	r := router.LoadRouters(
		controller.OrderController,
		controller.UserController,
		controller.AdminController,
		controller.SystemUserLevelController,
		controller.SystemUserTaskController,
		controller.PermissionController,
	)

	s := &http.Server{
		Addr:           ":" + cfg.Server.Port,
		Handler:        r,
		ReadTimeout:    120 * time.Second,
		WriteTimeout:   120 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	if err := s.ListenAndServe(); err != nil {
		log.Fatal(err)
	}

}

// func main() {
// 	log := logger.Factory(middleware.JWT)
// 	str := "test log"
// 	log.Debug(str)
// 	log.Debugf("%s\n", str)

// 	log.Info(str)
// 	log.Infof("%s\n", str)

// 	log.Warn(str)
// 	log.Warnf("%s\n", str)

// 	log.Fatal(str)
// 	log.Fatalf("%s\n", str)
// }
